package sxe.core.math;

import snobject.snInteger;
import snobject.snObject;
import snobject.snObjectArgList;
import step.FunctionWrapper;
import step.EvaluationFailure;

/**
 * Returns the closest snInteger to the argument.
 * The result is rounded to an integer by adding 1/2, taking the floor of the result, and casting the result to type snInteger.
 * In other words, the result is equal to the value of the expression: (snInteger)sxe.core.math.floor(a + 0.5d)
 * @param argv must contain an snDouble object at top of the stack
 * @return returns the value of the argument rounded to the nearest integer value
 */

public class round extends FunctionWrapper{
	
	public snObject Call(snObjectArgList argv) throws EvaluationFailure{
		// REQUIRES: argv must contain an snDouble object at top of the stack
		// EFFECTS: returns the snInteger of the value of the argument rounded to the nearest integer value
		double ret = argv.popDouble().getDbl();
		
		return new snInteger((int)Math.floor(ret+0.5));
	}
}